﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace OEMS
{
    public partial class ExportForm : Form
    {
        private List<Motor> motoren = new List<Motor>();
        private String[] onderdelen;
        
                
        public ExportForm()
        {
            InitializeComponent();
            onderdeelCbox.SelectedIndex = 0;
        }


        private void exportBtn_Click(object sender, EventArgs e)
        {
            
            // Prompt the user to enter a path/filename to save an example Excel file to
            int index = onderdelenLBox.SelectedIndex;
            saveFileDialog1.FileName = Export.setFileName(index, onderdeelCbox.SelectedItem.ToString(), motoren, null, null);
            saveFileDialog1.Filter = "Excel 2007 files (*.xlsx)|*.xlsx|Excel 97-2003 files (*.xls)|*.xls|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.OverwritePrompt = false;

            //  If the user hit Cancel, then abort!
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)
                return;

            string targetFilename = saveFileDialog1.FileName;

            try
            {
                
               
                if (onderdeelCbox.SelectedItem.Equals("Motor"))
                {
                    Motor motor = motoren[onderdelenLBox.SelectedIndex];
                    Export.ToFile(targetFilename, motor, "excel", true);
                    
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
                return;
            }

            if (openExcelCB.Checked)
            {
                Process p = new Process();
                p.StartInfo = new ProcessStartInfo(targetFilename);
                p.Start();
            }
            this.Close();
            
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void onderdeelCbox_SelectedIndexChanged(object sender, EventArgs e)
        {
            exportBtn.Enabled = false;
            onderdelenLBox.Items.Clear();

            if (onderdeelCbox.SelectedItem.Equals("Motor"))
            {
                
                motoren = MotorDB.GetMotors();
                onderdelen = new String[motoren.Count];

                for (int i = 0; i < motoren.Count; i++)
                {
                    onderdelen[i] = motoren[i].NominaalVermogen.ToString().Substring(0, 4) + "kW " + motoren[i].Klasse + " " + motoren[i].NominaalToerental.ToString().Substring(0, 4);

                }

                onderdelenLBox.Items.AddRange(onderdelen);
                }
        }

        private void onderdelenLBox_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            exportBtn.Enabled = true;
            //unchecken van listbox
            if (onderdelenLBox.CheckedItems.Count == 1)
            {
                Boolean isCheckedItemBeingUnchecked = (e.CurrentValue == CheckState.Checked);
                if (isCheckedItemBeingUnchecked)
                {
                    e.NewValue = CheckState.Checked;
                }
                else
                {
                    int checkedItemIndex = onderdelenLBox.CheckedIndices[0];
                    onderdelenLBox.ItemCheck -= onderdelenLBox_ItemCheck;
                    onderdelenLBox.SetItemChecked(checkedItemIndex, false);
                    onderdelenLBox.ItemCheck += onderdelenLBox_ItemCheck;
                }

                return;
            }
        }


    }
}
